package me.jessyan.armscomponent.commonres.widget.toast;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import me.jessyan.armscomponent.commonres.R;


/**
 * @author xuwei
 * @Date: 2020年8月26日17:56:21
 * @Description: 自定义Toast
 */
public class ToastView {
    private static ToastView mToastView;

    private ToastView() {
    }

    public static ToastView getInstance() {
        if (mToastView == null) {
            synchronized (ToastView.class) {
                if (mToastView == null) {
                    mToastView = new ToastView();
                }
            }
        }
        return mToastView;
    }

    public void showToast(Context context, int icon, String title, String brief) {
        View root = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.public_small_toast_layout, null);
        ImageView ivIcon = root.findViewById(R.id.iv_icon);
        TextView tvTitle = root.findViewById(R.id.tv_title);
        TextView tvBrief = root.findViewById(R.id.tv_brief);
        ivIcon.setImageResource(icon);
        tvTitle.setText(title);
        if (TextUtils.isEmpty(brief)) {
            tvBrief.setVisibility(View.GONE);
        } else {
            tvBrief.setText(brief);
        }
        Toast toast = new Toast(context.getApplicationContext());
        toast.setView(root);
        toast.show();
    }

    public void showToast(Context context, int icon, String title) {
        showToast(context.getApplicationContext(), icon, title, null);
    }

    public void showToastComplete(Context context, String title) {
        showToast(context.getApplicationContext(), R.drawable.public_ic_prompt_complete, title);
    }

    public void showToastError(Context context, String title) {
        showToast(context.getApplicationContext(), R.drawable.public_ic_prompt_error, title);
    }

    public void showToast(Context context,  String title) {
        showToast(context.getApplicationContext(), R.drawable.public_ic_prompt_normal, title, null);
    }
}
